<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class AutoReplyController extends Controller
{
    /**
     * 自动回复列表
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        $result['items'] = DB::table('autoreplys')->paginate(10);
//        dd($result);
        if(is_pjax()) {
            return View('admin.replylistsegment', $result);
        }

        return View('admin.replylist', $result);
    }

    /**
     * 添加自动回复页面
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
        $data = [];
        $data['edit_mode'] = 0;
        if(is_pjax()) {
            return View('admin.autoreplysegment', $data);
        }
        return View('admin.autoreply', $data);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
        $data['rulename'] = $request->get('rulename');
        $data['keyword'] = $request->get('keyword');
        $data['content'] = $request->get('content');
        $data['media_id'] = $request->get('media_id');
        $data['type'] = $request->get('type');
        $data['create_time'] = time();

        Log::info(''.$request->get('content'));

        $pattern = ["/<br>/","/<p>(.*?)<\/p>/ms"];
        $replaceTo = ["\n","\\1\r\n"];
        switch($data['type']) {
            case 'text':
                $data['media_id'] = '';

                // 格式化调整
                $data['content'] = preg_replace($pattern, $replaceTo, $data['content']);

                break;
            case 'image':
                $data['content'] = '';
                break;

            case 'news':
                $data['content'] = '';
                break;
        }

        // 入库
        $id = DB::table('autoreplys')->insertGetId($data);
        if($id) {
            return response()->json(['code'=>2000]);
        }else {
            return response()->json(['code'=>2001]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
        $data = [];
        $data['edit_mode'] = 1;
        $data['id'] = $id;

        $data['replyinfo'] = DB::table('autoreplys')->where('id', $id)->first();
        //$data['menuinfo'] = Menus::getMenuDetail($id);
        //$data['headmenus'] = Menus::getHeadMenu();
        if($data['replyinfo']->type=='image') {
            $imageinfo = DB::table('picuploads')->where('media_id', $data['replyinfo']->media_id)->first();
            if(!empty($imageinfo->weixinurl)) {
                $data['image'] = $imageinfo->weixinurl;
            }else {
                $data['image'] = '';
            }
        }else if($data['replyinfo']->type=='news') {
            $newsinfo = DB::table('news')->where('media_id',$data['replyinfo']->media_id)->orderby('id', 'asc')->first();
            $data['news']['title'] = $newsinfo->title;
            $thumb = DB::table('picuploads')->where('media_id', $newsinfo->thumb_media_id)->first();
            $data['news']['thumb'] = $thumb->weixinurl;
        }

        if(is_pjax()) {
            return View('admin.autoreplysegment', $data);
        }
        return View('admin.autoreply', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //2000
        //data: { content: $content, media_id: $media_id, type: $type },
        $data['rulename'] = $request->get('rulename');
        $data['keyword'] = $request->get('keyword');
        $data['content'] = $request->get('content');
        $data['media_id'] = $request->get('media_id');
        $data['type'] = $request->get('type');
        if($data['type'] == 'text') {
            $pattern = ["/<br>/","/<p>(.*?)<\/p>/ms"];
            $replaceTo = ["\n","\\1\r\n"];
            // 格式化调整
            $data['content'] = preg_replace($pattern, $replaceTo, $data['content']);
        }
        if( empty($data['rulename']) || empty($data['keyword']) || empty($data['type']) ) {
            return response()->json(['code'=>2001, 'message'=>'各项再检查下是不是有遗漏呀~']);
        }else {

            $form_data = [
                'rulename' => $data['rulename'],
                'keyword' => $data['keyword'],
                'content' => $data['content'],
                'media_id' => $data['media_id'],
                'type' => $data['type'],
            ];

            $result = DB::table('autoreplys')->where('id', $id)->update($form_data);
            if($result) {
                return response()->json(['code'=>2000, 'message'=> '自动回复更新成功！']);
            }else {
                return response()->json(['code'=>2002, 'message'=>'自动回复更新失败']);
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //replyid
        $replyid = \Input::get('replyid');
        //$result = Menus::deleteMenu($menuid);
        $result = DB::table('autoreplys')->where('id', $replyid)->delete();
        if($result) {
            return response()->json(['code'=>2000, 'message'=> '删除菜单成功！']);
        }else {
            return response()->json(['code'=>2002, 'message'=>'删除菜单失败！']);
        }
    }
}
